package com.sms.controller;

import cn.hutool.json.JSONUtil;
import com.sms.common.A;
import com.sms.pojo.Users;
import com.sms.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//用户登录
@WebServlet("/UsersLogin")
public class UsersLoginController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username=request.getParameter("username");
        String password=request.getParameter("password");

        if (username == null || username.isEmpty()) {
            response.getWriter().write(JSONUtil.toJsonStr(A.fail("用户名不能为空")));
        }

        if (password == null || password.isEmpty()) {
            response.getWriter().write(JSONUtil.toJsonStr(A.fail("密码不能为空")));
        }

        Users user=new Users();
        user.setUsername(username);
        user.setPassword(password);

        UsersServiceImpl impl=new UsersServiceImpl();
        if(impl.login(user)) {
            response.getWriter().write(JSONUtil.toJsonStr(A.success("登录成功")));
            impl.getSqlSession().close();
        } else {
            response.getWriter().write(JSONUtil.toJsonStr(A.fail("登录失败")));
            impl.getSqlSession().close();
        }
    }
}


